package com.joyoung.sweep.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;

import com.joyoung.sweep.config.Cons;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * APP 相关工具类
 */
public class AppUtils {


    /**
     * 获取App 版本信息
     * @param context
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    public static String getVersion(Context context){
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "4.7.12";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
            //出现异常则返回某一个版本
            versionName = "1.0.0";
        }

        return versionName;
    }

    public static String getAppToken(Context context) {
        return (String)SPUtils.get(context, Cons.SP_APP_AUTHOR_TOKEN, "0000");
    }

    public static String getVersionCode(Context context) {

        try {
            // 获取packagemanager的实例
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo;
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            int version = packInfo.versionCode;
            return version + "";
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static Typeface getNumberFont(Context context) {
        // Font path
        String fontPath = "fonts/DINCond-Medium.otf";
        // Loading Font Face
        return Typeface.createFromAsset(context.getAssets(), fontPath);
    }

    /**
     * Px  转 dp
     * @param context
     * @param val
     * @return
     */
    public static float px2dp(Context context, float val) {
        float density = context.getResources().getDisplayMetrics().density;
        return val * density;
    }


    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * 判断是否连接上wifi
     */
    public static boolean connByWifi(Activity at) {
        if (at == null) {
            return false;
        }
        boolean isConnect = false;
        ConnectivityManager cm = (ConnectivityManager) at
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        }
        if (null != cm.getActiveNetworkInfo()) {
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if (activeNetwork != null) {
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                    isConnect = activeNetwork.isConnected();
                }
            }
        }
        return isConnect;
    }

    /**
     * 检查是否能连接网络
     */
    public static boolean check_connect_net(Context con) {
        ConnectivityManager cwjManager = (ConnectivityManager) con
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            if (cwjManager.getActiveNetworkInfo() != null
                    && cwjManager.getActiveNetworkInfo().isAvailable()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
